package com.mofang.powerdekorhelper.activity.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.adapter.ActivityManagerListAdapter;
import com.mofang.powerdekorhelper.adapter.PastActivityAdapter;
import com.mofang.powerdekorhelper.base.MvpActivity;
import com.mofang.powerdekorhelper.base.recycleview.BaseRecycleViewList;
import com.mofang.powerdekorhelper.model.ActivityList;
import com.mofang.powerdekorhelper.model.PastActivityList;
import com.mofang.powerdekorhelper.model.ResultMessage4;
import com.mofang.powerdekorhelper.persenter.ActiveListPersenter;
import com.mofang.powerdekorhelper.utils.LoadPrograss;
import com.mofang.powerdekorhelper.utils.SharePerforenceUtils;
import com.mofang.powerdekorhelper.utils.T;
import com.mofang.powerdekorhelper.view.ActivityListView;
import com.mofang.powerdekorhelper.witget.TitleBar;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by luofei on 2017/8/23 0023.
 */

public class ActivityListAct extends MvpActivity<ActivityListView, ActiveListPersenter> implements ActivityListView {

    @BindView(R.id.activity_list_titlebar)
    TitleBar mTitleBar;

    @BindView(R.id.activity_list_search_edit)
    EditText searchEdit;

    @BindView(R.id.activity_list_current_list)
    BaseRecycleViewList cActivityRecycle;

    @BindView(R.id.activity_list_past_list)
    BaseRecycleViewList pctivityRecycle;

    private LoadPrograss loadPrograss;

    private int page = 1;
    private int pageSize = 10;
    private Integer shopId;

    @Override
    public void initView() {
        inflateLayout(R.layout.activity_list_layout);
        initTitleBarWithback(mTitleBar, R.string.activity_list);
        loadPrograss = new LoadPrograss(this);

        cActivityRecycle.setLayoutManager(new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });

        pctivityRecycle.setLayoutManager(new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
    }

    @Override
    public void initData() {

        shopId = SharePerforenceUtils.getInstance(this).getShopId();
        presenter.getActivityList(page, pageSize, shopId, getKeyWord());
        presenter.getPastActivityList(page, pageSize, shopId);

        searchEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    page = 1;
                    presenter.getActivityList(page, pageSize, shopId, getKeyWord());
                }
                return false;
            }
        });
    }

    @OnClick({R.id.activity_list_search_btn})
    void onclick(View v) {
        switch (v.getId()) {
            case R.id.activity_list_search_btn:
                page = 1;
                presenter.getActivityList(page, pageSize, shopId, getKeyWord());
                break;
        }
    }

    @Override
    public void setActivityList(final ActivityList activityList) {

        // 当前活动
        ActivityManagerListAdapter adapter = new ActivityManagerListAdapter(activityList.getResult().getData(), this, R.layout.activity_manager_list_item_layout);

        cActivityRecycle.setAdapter(adapter);

        hideProgress();
    }

    @Override
    public void setPastActivityList(PastActivityList pastActivityList) {
        // 往期活动
        PastActivityAdapter activityListAdapter = new PastActivityAdapter(pastActivityList.getData(), this, R.layout.activity_manager_list_item_layout);
        pctivityRecycle.setAdapter(activityListAdapter);
    }

    private String getKeyWord() {
        String keyWord = searchEdit.getText().toString();
        return keyWord;
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void toast(CharSequence s) {
        T.showShort(this, s);
    }

    @Override
    public void toast(int id) {

    }

    @Override
    public void showNullLayout() {
        hideProgress();
    }

    @Override
    public void hideNullLayout() {
        hideProgress();
    }

    @Override
    public void showErrorLayout(View.OnClickListener listener) {

    }

    @Override
    public void hideErrorLayout() {
        hideProgress();
    }

    @Override
    public void onError(String errorInfo) {
        hideProgress();
    }

    @Override
    public ActiveListPersenter initPresenter() {
        return new ActiveListPersenter();
    }

    private String getsearchWod() {

        String searchWord = searchEdit.getText().toString();

        return searchWord;
    }

    @Override
    public void setNode3Image(ResultMessage4 node3ImageUrl) {

    }
}
